package hero.mps.web.sys_menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hero.mps.web.sys_menu.entity.SysMenu;
import hero.mps.web.sys_menu.mapper.SysMenuMapper;
import hero.mps.web.sys_menu.service.SysMenuService;
import hero.mps.web.sys_menu.utils.MakeTree;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Override
    public List<SysMenu> getList() {
        // 查询所有菜单
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> list = this.baseMapper.selectList(queryWrapper);
        // 组装树
        List<SysMenu> sysMenus = MakeTree.makeMenuTree(list, 0);
        return sysMenus;
    }

    @Override
    public List<SysMenu> getParentList() {
        // 查询目录和菜单
        String [] types = {"0", "1"};
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysMenu::getType, Arrays.asList(types));
        List<SysMenu> menuList = this.baseMapper.selectList(queryWrapper);
        // 构建顶级菜单
        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentId(-1);
        sysMenu.setMenuId(0);
        sysMenu.setTitle("顶级菜单");
        menuList.add(sysMenu);
        // 构建树数据
        List<SysMenu> list = MakeTree.makeMenuTree(menuList, -1);
        return list;
    }

    @Override
    public List<SysMenu> getMenuByUserId(Integer userId) {
        return this.baseMapper.getMenuByUserId(userId);
    }

    @Override
    public List<SysMenu> getMenuByRoleId(Integer roleId) {
        return this.baseMapper.getMenuByRoleId(roleId);
    }

    @Override
    public List<SysMenu> getMenuByStuId(Integer stuId) {
        return this.baseMapper.getMenuByStuId(stuId);
    }

    @Override
    public List<SysMenu> getMenuByTeacherId(Integer teacherId) {
        return this.baseMapper.getMenuByTeacherId(teacherId);
    }
}
